package com.flute.datareceiver.data.unit.test;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.FileEntity;
import org.apache.http.impl.client.DefaultHttpClient;

public class HttpPostTestIndex {
	private static Log log = LogFactory.getLog(HttpPostTestIndex.class);
	private static final String Dir = "E:\\安装软件\\data\\albums\\albums\\album1.xml";
	
	//private static final String Dir = "E:\\work\\FAST\\新搜索\\albums_fz_20101116113723781.xml";

	public void postFileToRemote(File file) throws Exception {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateStr = "2008-07-23 00:00:00";
		Date date = new Date();
		date = format.parse(dateStr);
		System.out.println(date.getTime());
		HttpPost post = new HttpPost(
				"http://localhost:8080/datareceiver/datareceiver?type=album");
			//"http://192.168.1.125:7080/datainterface/datareceiver?type=song");
				//	"http://192.168.1.126:7080/datainterface/datareceiver?type=song");
				//"http://218.200.227.187/searcher/songServlet");
		/*
		 * HttpPost post = new HttpPost(
		 * "http://192.168.1.110:8080/datainterface/datareciever?type=song");
		 */
		// HttpPost post = new
		// HttpPost("http://218.200.227.187/search/AlbumServlet");
		FileEntity entity = new FileEntity(file, "text/plain;charset=\"UTF-8\"");
		post.setEntity(entity);
		HttpClient client = new DefaultHttpClient();
		HttpResponse res = client.execute(post);
		HttpEntity e2 = res.getEntity();
		if (e2 == null) {
			log.info("[ " + file.getName()
					+ "] post request exception and res info "
					+ res.getStatusLine());
		}
	}

	public static void main(String[] args) throws Exception {
		/*Timer timer = new Timer(); 
		timer.schedule(new TimerTask() {
	           public void run() {
	        	   try {
					new HttpPostTestIndex().startPost();
				} catch (Exception e) {
					// TODO: handle exception
					log.info("e:"+e);
				}
	           }}, 0, 1000*60*2);*/
		new HttpPostTestIndex().startPost();
		
	}

	public void startPost() throws Exception {
		/*Random random = new Random();
		int k = random.nextInt();
		int j = Math.abs(k % 100);
		log.info(Dir+"\\singer"+j+".xml");
		File filedir = new File(Dir+"\\singer"+j+".xml");	*/	
		File filedir = new File(Dir);	
		if (filedir.isDirectory()) {
			File[] files = filedir.listFiles();
			int i =1;
			for (File f : files) {
				log.info("f:"+f);
				postFileToRemote(f);
				log.info("成功:---"+i++);
			}
		} else {
			postFileToRemote(filedir);
			log.info("成功:---"+filedir);
		}

	}
}
